
var fs = require("fs");
var util = require("util");
var http = require("../module/http");
var exception = require('./exception');

const api_host = "http://api.fangmm.com/v3";
//const api_host = "http://alpha.fangmm.com/api3/v3";
const header   = {};

var token_format = "bearer %s";
function buildHeader(token)
{
    if(token){
        var header = {};
        header["Authorization"] = util.format(token_format,token);
        return header;
    }
    return null;
}
var team_api_format = '%s/teams/%s?app=share';
function forTeam() {

    this.teamId = '';

    this.call = function () {

        var url = util.format(team_api_format, api_host, this.teamId);
        return function(callback) {
            log.http('team url: %s', url);
            http.get(url, header, function(err, res, body) {
                if(err){
                    return callback(exception(exception.DBError, err.message));
                }
                log.http('team res: %s', body);
                callback(null, JSON.parse(body));
            })
        }
    }
}

var user_api_format = '%s/users/%s?app=share';
function forUser() {

    this.userId = '';

    this.call = function () {

        var url = util.format(user_api_format, api_host, this.userId);
        return function(callback) {
            log.http('user url: %s', url);
            http.get(url, header, function(err, res, body) {
                if(err){
                    return callback(exception(exception.DBError, err.message));
                }
                log.http('user res: %s', body);
                callback(null, JSON.parse(body));
            })
        }
    }
}

var house_api_format = '%s/houses/%s?app=share';
function forHouse() {

    this.houseId = '';

    this.call = function () {

        var url = util.format(house_api_format, api_host, this.houseId, this.userId);
        return function(callback) {
            log.http('house url: %s', url);
            http.get(url, header, function(err, res, body) {
                if(err){
                    return callback(exception(exception.DBError, err.message));
                }
                log.http('house res: %s', body);
                callback(null, JSON.parse(body));
            })
        }
    }
}

var group_api_format = '%s/teams/houses/%s?app=share';
function forGroup() {

    this.houseId = '';

    this.call = function () {

        var url = util.format(group_api_format, api_host, this.houseId);
        return function(callback) {
            log.http('group url: %s', url);
            http.get(url, header, function(err, res, body) {
                if(err){
                    return callback(exception(exception.DBError, err.message));
                }
                log.http('group res: %s', body);
                callback(null, JSON.parse(body));
            })
        }
    }
}

var favorite_api_format = '%s/favorites/houses/%s?app=share';
function forFavorite() {

    this.houseId = '';

    this.call = function () {

        var url = util.format(favorite_api_format, api_host, this.houseId);
        return function(callback) {
            log.http('favorite url: %s', url);
            http.get(url, header, function(err, res, body) {
                if(err){
                    return callback(exception(exception.DBError, err.message));
                }
                log.http('favorite res: %s', body);
                callback(null, JSON.parse(body));
            })
        }
    }
}

var user_team_api_format = '%s/teams/users/%s?app=share';
function forUserTeam() {

    this.userId = '';

    this.call = function () {

        var url = util.format(user_team_api_format, api_host, this.userId);
        return function(callback) {
            log.http('favorite url: %s', url);
            http.get(url, header, function(err, res, body) {
                if(err){
                    return callback(exception(exception.DBError, err.message));
                }
                log.http('favorite res: %s', body);
                callback(null, JSON.parse(body));
            })
        }
    }
}

var community_api_format = '%s/communities/%s?app=share';
function forCommunity() {

    this.communityId = '';

    this.call = function () {

        var url = util.format(community_api_format, api_host, this.communityId);
        return function(callback) {
            log.http('communityId url: %s', url);
            http.get(url, header, function(err, res, body) {
                if(err){
                    return callback(exception(exception.DBError, err.message));
                }
                log.http('communityId res: %s', body);
                callback(null, JSON.parse(body));
            })
        }
    }
}

var surrounding_api_format = '%s/communities/%s/surroundings?app=share';
function forSurrounding() {

    this.communityId = '';

    this.call = function () {

        var url = util.format(surrounding_api_format, api_host, this.communityId);
        return function(callback) {
            log.http('surrounding url: %s', url);
            http.get(url, header, function(err, res, body) {
                if(err){
                    return callback(exception(exception.DBError, err.message));
                }
                log.http('surrounding res: %s', body);
                callback(null, JSON.parse(body));
            })
        }
    }
}

var houseList_api_format = '%s/houses?app=share&cityCode=%s&communityId=%s';
function forHouseList() {

    this.communityId = '';
    this.cityCode = 3502;

    this.call = function () {

        var url = util.format(houseList_api_format, api_host,this.cityCode, this.communityId);
        return function(callback) {
            log.http('surrounding url: %s', url);
            http.get(url, header, function(err, res, body) {
                if(err){
                    return callback(exception(exception.DBError, err.message));
                }
                log.http('surrounding res: %s', body);
                callback(null, JSON.parse(body));
            })
        }
    }
}

var panoramic_api_format = '%s/images/panoramas?houseId=%s';
function forPanoramic() {

    this.houseId = '';

    this.call = function () {

        var url = util.format(panoramic_api_format, api_host, this.houseId);
        return function(callback) {
            log.http('panoramic url: %s', url);
            http.get(url, header, function(err, res, body) {
                if(err){
                    return callback(exception(exception.DBError, err.message));
                }
                log.http('panoramic res: %s', body);
                callback(null, JSON.parse(body));
            })
        }
    }
}

var collection_api_format = '%s/favorites/users/%s';
function forCollection() {

    this.userId = '';

    this.call = function () {

        var url = util.format(collection_api_format, api_host, this.userId);
        return function(callback) {
            log.http('collection url: %s', url);
            http.get(url, header, function(err, res, body) {
                if(err){
                    return callback(exception(exception.DBError, err.message));
                }
                log.http('collection res: %s', body);
                callback(null, JSON.parse(body));
            })
        }
    }
}

var house_img_api_format = '%s/images/houses';
function forHouseImg() {

    this.userId = '';
    this.call = function (files) {

        var url = util.format(house_img_api_format, api_host);
        var data = {};
        data.files = [];
        Object.keys(files).forEach(function(key) {
            data.files.push(fs.createReadStream(files[key].path))
        })
        return function(callback) {
            log.http('house_img url: %s', url);
            http.postFile(url, data, header, function(err, res, body) {
                if(err){
                    return callback(exception(exception.DBError, err.message));
                }
                log.http('house_img res: %s', body);
                callback(null, JSON.parse(body));
            })
        }
    }
}

var house_post_api_format = '%s/houses';
var params = ['community', 'rent', 'rentTime', 'comment', 'roomNum', 'hallNum', 'toiletNum', 'userId'];
function forPostHouse() {

    var self   = this;
    params.forEach(function(param) {
        self[param] = '';
    });
    this.cityCode = 3502;
    this.fileIds = [];

    this.call = function () {

        var data = {};
        data.cityCode = this.cityCode;
        data.fileIds = this.fileIds;
        params.forEach(function(param) {
            data[param] = self[param];
        });

        var url = util.format(house_post_api_format, api_host);
        return function(callback) {
            log.http('house_post url: %s', url);
            http.post(url, data, header, function(err, res, body) {
                if(err){
                    return callback(exception(exception.DBError, err.message));
                }
                log.http('house_img res: %s', body);
                callback(null, JSON.parse(body));
            })
        }
    }
}

var add_api_format = '%s/cms/ads?cityCode=%s&classify=%s';
function forAdd() {

    this.classify = '';
    this.cityCode = 3502;

    this.call = function () {

        var url = util.format(add_api_format, api_host,this.cityCode, this.classify);
        return function(callback) {
            log.http('add url: %s', url);
            http.get(url, header, function(err, res, body) {
                if(err){
                    return callback(exception(exception.DBError, err.message));
                }
                log.http('add res: %s', body);
                callback(null, JSON.parse(body));
            })
        }
    }
}

var code_api_format = '%s/sms/user/register';
function forCode() {

    this.mobile = '';

    this.call = function () {

        var data = {};
        data.mobile = this.mobile;
        var url = util.format(code_api_format, api_host);
        return function(callback) {
            log.http('code url: %s', url);
            http.post(url, data, header, function(err, res, body) {
                if(err){
                    return callback(exception(exception.DBError, err.message));
                }
                log.http('code res: %s', body);
                callback(null, JSON.parse(body));
            })
        }
    }
}

var login_api_format = '%s/users';
function forLogin() {

    this.mobile = '';
    this.vcode  = '';
    this.app  = 'm';

    this.call = function () {

        var data = {};
        data.mobile = this.mobile;
        data.vcode = this.vcode;
        data.app = this.app;
        var url = util.format(login_api_format, api_host);
        return function(callback) {
            log.http('login url: %s', url);
            http.post(url, data, header, function(err, res, body) {
                if(err){
                    return callback(exception(exception.DBError, err.message));
                }
                log.http('login res: %s', body);
                callback(null, JSON.parse(body));
            })
        }
    }
}

var requirement_api_format = '%s/requirements/lastest?userId=%s&app=%s';
function forRequirement() {

    this.userId = '';
    this.token = '';
    this.app  = 'm';

    this.call = function () {
        var header = buildHeader(this.token);
        var url = util.format(requirement_api_format, api_host, this.userId, this.app);
        return function(callback) {
            log.http('Requirement url: %s', url);
            http.get(url ,header , function(err, res, body) {
                if(err){
                    return callback(exception(exception.DBError, err.message));
                }
                log.http('Requirement res: %s', body);
                callback(null, JSON.parse(body));
            })
        }
    }
}

var auth_api_format = '%s/users';
var winxinArray = ['openid', 'accessToken', 'name', 'avatar', 'gender', 'country', 'province', 'city'];
function forWinxinAuth() {

    var self = this;
    winxinArray.forEach(function(key) {
        self[key] = '';
    });
    this.oauth  = 'wx';
    this.app  = 'm';

    this.call = function (callback) {

        var data = {};
        data.oauth = this.oauth;
        data.app = this.app;
        winxinArray.forEach(function(key) {
            data[key] = self[key];
        });
        var url = util.format(auth_api_format, api_host);

        console.log('WinxinAuth url: %s', url);
        http.post(url, data, header, function(err, res, body) {
            if(err){
                return callback(exception(exception.DBError, err.message));
            }
            console.log('WinxinAuth res: %s', body);
            callback(null, JSON.parse(body));
        })

    }
}

exports.forTeam             = forTeam;
exports.forUser             = forUser;
exports.forHouse            = forHouse;
exports.forGroup            = forGroup;
exports.forFavorite         = forFavorite;
exports.forUserTeam         = forUserTeam;
exports.forCommunity        = forCommunity;
exports.forSurrounding      = forSurrounding;
exports.forHouseList        = forHouseList;
exports.forPanoramic        = forPanoramic;
exports.forCollection       = forCollection;
exports.forHouseImg         = forHouseImg;
exports.forPostHouse        = forPostHouse;
exports.forAdd              = forAdd;
exports.forCode             = forCode;
exports.forLogin            = forLogin;
exports.forRequirement      = forRequirement;
exports.forWinxinAuth       = forWinxinAuth;